/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mundo;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

/**
 *
 * @author cesarfgt
 */
public class PanelMundo extends javax.swing.JPanel {

    /**
     * Creates new form PanelMundo
     */
    private int escala;
    private Malla malla;
    private ArrayList<Tramo> PendTramos;    //  Tramos pendients por dibujar
    private ArrayList<Cruce> PendCruce;
    

    public PanelMundo() {
        initComponents();
        this.malla = null;
        this.PendCruce = new ArrayList<Cruce>();
        this.PendTramos = new ArrayList<Tramo>();
        this.escala = 10;        
    }

    @Override
    public void paintComponent(Graphics g) {
        if (this.malla != null) {
            Graphics2D g2 = (Graphics2D) g; // cast g to Graphics2D  
            g2.setColor(new Color(0, 0, 255, 100));
            while(PendCruce.size()>0){
                Cruce cr = PendCruce.remove(0);
                g2.fillRect((cr.getInfIz().getX()-1)*escala, (cr.getInfIz().getY()-1)*escala, (cr.getSupDer().getX() - cr.getInfIz().getX() + 1)*escala,(cr.getSupDer().getY() - cr.getInfIz().getY() + 1)*escala);
            }
            g2.setColor(new Color(255, 0, 0, 100));
            while(PendTramos.size()>0){
                Tramo Tr = PendTramos.remove(0);
                g2.fillRect((Tr.getInfIz().getX()-1)*escala, (Tr.getInfIz().getY()-1)*escala, (Tr.getSupDer().getX() - Tr.getInfIz().getX() + 1)*escala,(Tr.getSupDer().getY() - Tr.getInfIz().getY() + 1)*escala);
            }
            
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    /**
     * @param malla the malla to set
     */
    public void setMalla(Malla malla) {
        this.malla = malla;
        this.PendCruce.clear();
        this.PendTramos.clear();
        
        this.escala = Math.min(this.getPreferredSize().width/this.malla.getMaxX(),this.getPreferredSize().height/this.malla.getMaxY());
        
        for (int i = 0; i < this.malla.getListaCruces().size(); i++) {
            this.PendCruce.add(this.malla.getListaCruces().get(i));
        }
        for (int i = 0; i < this.malla.getListaTramos().size(); i++) {
            this.PendTramos.add(this.malla.getListaTramos().get(i));
        }
    }
}
